<template>
  <div class="app-container">
    <el-card>
      <div slot="header" class="clearfix">
        <span>员工列表</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="handleAdd">添加员工</el-button>
      </div>

      <el-table :data="list" border style="width: 100%">
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="name" label="姓名"></el-table-column>
        <el-table-column prop="position" label="职位"></el-table-column>
        <el-table-column prop="department" label="部门"></el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleDetail(scope.row.id)">详情</el-button>
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="50%">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="职位" prop="position">
          <el-input v-model="form.position"></el-input>
        </el-form-item>
        <el-form-item label="部门" prop="department">
          <el-input v-model="form.department"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="员工详情" :visible.sync="detailVisible" width="60%">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="姓名">{{ currentEmployee.name }}</el-descriptions-item>
        <el-descriptions-item label="职位">{{ currentEmployee.position }}</el-descriptions-item>
        <el-descriptions-item label="部门">{{ currentEmployee.department }}</el-descriptions-item>
      </el-descriptions>

      <el-divider></el-divider>
      <h4>服务记录</h4>
      <el-table :data="serviceRecords" border style="width: 100%">
        <el-table-column prop="serviceType" label="服务类型"></el-table-column>
        <el-table-column prop="serviceTime" label="服务时间"></el-table-column>
        <el-table-column prop="description" label="描述"></el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { getEmployeeList, addEmployee, updateEmployee, deleteEmployee, getEmployeeDetail } from '@/api/employee'

export default {
  data() {
    return {
      list: [],
      serviceRecords: [],
      currentEmployee: {},
      dialogVisible: false,
      detailVisible: false,
      dialogTitle: '',
      form: {
        id: null,
        name: '',
        position: '',
        department: ''
      },
      rules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        position: [{ required: true, message: '请输入职位', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      getEmployeeList().then(response => {
        this.list = response.data
      })
    },
    handleAdd() {
      this.dialogTitle = '添加员工'
      this.form = {
        id: null,
        name: '',
        position: '',
        department: ''
      }
      this.dialogVisible = true
    },
    handleEdit(row) {
      this.dialogTitle = '编辑员工'
      this.form = Object.assign({}, row)
      this.dialogVisible = true
    },
    handleDelete(id) {
      this.$confirm('确认删除该员工?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteEmployee(id).then(() => {
          this.$message.success('删除成功')
          this.fetchData()
        })
      })
    },
    handleDetail(id) {
      getEmployeeDetail(id).then(response => {
        this.currentEmployee = response.data
        this.serviceRecords = response.data.serviceRecords
        this.detailVisible = true
      })
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.form.id) {
            updateEmployee(this.form).then(() => {
              this.$message.success('更新成功')
              this.dialogVisible = false
              this.fetchData()
            })
          } else {
            addEmployee(this.form).then(() => {
              this.$message.success('添加成功')
              this.dialogVisible = false
              this.fetchData()
            })
          }
        }
      })
    }
  }
}
</script>
